package com.letoken.platform.admin.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.admin.service.AdminMallService;
import com.letoken.platform.pub.annotation.auth.NotAuthorization;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.annotation.pub.Trim;
import com.letoken.platform.pub.constant.ConstantsValue;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.dao.WithdrawMapper;
import com.letoken.platform.repo.admin.po.Withdraw;
import com.letoken.platform.repo.admin.req.mall.*;
import com.letoken.platform.repo.admin.req.user.*;
import com.letoken.platform.repo.admin.res.mall.*;
import com.letoken.platform.repo.admin.res.user.EditWithdrawYesRes;
import com.letoken.platform.repo.admin.res.user.NewWithdrawListRes;
import com.letoken.platform.repo.admin.res.user.QueryAppWithdrawListRes;
import com.letoken.platform.repo.admin.res.user.QueryLineChartRes;
import com.letoken.platform.repo.app.dao.AppUserIssueRecordMapper;
import com.letoken.platform.repo.mall.dao.*;
import com.letoken.platform.repo.mall.po.*;
import com.letoken.platform.repo.utlis.LocalBlockUtils;
import com.letoken.platform.repo.utlis.PageDataUtil;
import com.letoken.platform.repo.utlis.lock.Lock;
import com.letoken.platform.repo.utlis.lock.LockCategoryEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "钱包交互接口")
@Slf4j
public class AdminWalletController {

    @Autowired
    private AdminMallService adminMallService;

    @Autowired
    private AppMallBannerMapper appMallBannerMapper;
    @Autowired
    private AppMallMenuConfigMapper appMallMenuConfigMapper;
    @Autowired
    AppUserIssueRecordMapper appUserIssueRecordMapper;
    @Autowired
    AppMallInfoMapper appMallInfoMapper;
    @Autowired
    AdminMallIssueConfigMapper adminMallIssueConfigMapper;
    @Autowired
    AdminMallConfigMapper adminMallConfigMapper;

    @Resource
    WithdrawMapper withdrawMapper;


    @Permission( uri = "/withdraw/list",qx = "/withdraw/list")
    @Trim
    @PostMapping("/withdraw/list")
    @ApiOperation(value = "提现记录", notes = "提现记录")
    public LocalBlockUtils.BaseResp<String> withdrawalsList(@RequestBody @Validated NewWithdrawListReq req) {
        LocalBlockUtils.WithDrawApplyListDto dto =   LocalBlockUtils.WithDrawApplyListDto.builder().build();
        BeanUtils.copyProperties(req,dto);
        return LocalBlockUtils.withdrawApplyList(dto);
    }


    @Permission(uri = "/withdraw/approve",qx = "/withdraw/approve")
    @Trim
    @PostMapping("/withdraw/approve")
    @ApiOperation(value = "钱包提现审核", notes = "钱包提现审核")
    public LocalBlockUtils.BaseResp<String> walletAudit(@RequestBody @Validated NewWithdrawAuditReq req) {
        LocalBlockUtils.WithDrawAuditDto dto =   LocalBlockUtils.WithDrawAuditDto.builder().build();
        BeanUtils.copyProperties(req,dto);
        return LocalBlockUtils.withdrawAudit(dto);

    }


    @Permission(uri = "/withdraw/CAPapply",qx = "/withdraw/CAPapply")
    @Trim
    @PostMapping("/withdraw/CAPapply")
    @ApiOperation(value = "CAP提现审核", notes = "CAP提现审核")
    @Lock(ce = LockCategoryEnum.DEFAULT,value = "CAPapply",field = "userId",expire = 15l)
    public LocalBlockUtils.BaseResp<String> CAPapply(@RequestBody @Validated NewWithdrawAuditReq req) {
        log.info("进入CAP提现审核请求参数:"+ JSON.toJSONString(req));
        LocalBlockUtils.WithDrawAuditDto dto =   LocalBlockUtils.WithDrawAuditDto.builder().build();
        BeanUtils.copyProperties(req,dto);
        Withdraw withdraw = withdrawMapper.selectOne(new QueryWrapper<Withdraw>().eq("sn",req.getSeqNo()));
        log.info("进入CAP提现审核  查询数据:"+ JSON.toJSONString(req));
        if(!withdraw.getTokenId().equals(4) || !withdraw.getTransferMode().equals(1) || !withdraw.getStatus().equals(0)){
            throw new RuntimeException("此接口,只用于cap链上提现");
        }
        log.info("进入CAP提现审核  开始调用钱包借口:"+ JSON.toJSONString(dto));
        return LocalBlockUtils.withdrawAuditCAP(dto);
    }

}
